/*
 * @Author: your name
 * @Date: 2021-08-10 13:56:12
 * @LastEditTime: 2021-08-13 17:39:33
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \mqttsWithLwip\Core\user\threadMgr\threadmgr.c
 */


/* This is a small demo of the high-performance ThreadX kernel.  It includes examples of eight
   threads of different priorities, using a message queue, semaphore, mutex, event flags group, 
   byte pool, and block pool.  */



#include "main.h"
#include "crc.h"
#include "eth.h"
#include "rng.h"
#include "usart.h"
#include "gpio.h"
#include "fsmc.h"
#include <stdio.h>
#include "tx_api.h"
#include "nt35510.h"
#include "netthread.h"
#include "uithread.h"
/* Define what the initial system looks like.  */
extern void SystemClock_Config(void);


/* Define the ThreadX object control blocks...  */
#define THREAD_STACK_SIZE	1024
const static char *thread_name = "MAIN";
static TX_THREAD        thread_handle;
static UCHAR            thread_stack[THREAD_STACK_SIZE];



#define THREADFIFO_SIZE 4096
TX_BYTE_POOL threadmgr_mempool;
UCHAR  threadmgr_mempool_mem[THREADFIFO_SIZE];

volatile UCHAR *eSram = (UCHAR *)0X68000000;
#define ESRAM_SIZE (1024*1024*1)
TX_BYTE_POOL esram_mempool;

#define ESRAM_POOL_SIZE (ESRAM_SIZE-100)


 static void thread_entry(ULONG thread_input)
 {
	LONG *date;
	UINT status;


	SystemClock_Config();
	MX_GPIO_Init();
	MX_USART1_UART_Init();
	MX_CRC_Init();
	MX_RNG_Init();
	MX_FSMC_Init();
	NT35510_Init();
	
	printf("%s \r\n",_tx_version_id);

	tx_byte_pool_create(&threadmgr_mempool, "thread pool", threadmgr_mempool_mem, sizeof(threadmgr_mempool_mem));
	tx_byte_pool_create(&esram_mempool, "esram pool", (UCHAR *)eSram, ESRAM_SIZE);

	status = tx_byte_allocate(&esram_mempool,(VOID **) &date, ESRAM_POOL_SIZE, TX_NO_WAIT);
	if(status != TX_SUCCESS)
	{
		printf("mem allocate err[%d]!!!\r\n",status);
	}

	for(ULONG i=0;i<ESRAM_POOL_SIZE/4;i++)
	{
		date[i]=0x555555;
	}

	for(ULONG i=0;i<ESRAM_POOL_SIZE/4;i++)
	{
		if(date[i]!= 0x555555)
		{
			printf("sram err[%ld]!!!!\r\n",((i/1024)/1024));
			break;
		}

	}
	printf("bsp init succ!!!\r\n");
	thread_net_create();
	thread_ui_create();
     while(1)
     {
         tx_thread_sleep(1000);
     }
 }



void main_thread_create(void)
{

	UINT status;
	status= tx_thread_create(&thread_handle,
					(CHAR *)thread_name,
					thread_entry,
					0,
					thread_stack,
					THREAD_STACK_SIZE,
					0,
					0,
					TX_NO_TIME_SLICE,
					TX_AUTO_START);
	printf("create thread %s==>[%d]!\r\n",thread_name,status);
}





void   tx_application_define(void *first_unused_memory)
{
	main_thread_create();
}

